package web.zz.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import web.zz.aop.LogAnnotation;
import web.zz.bean.Log_info;
import web.zz.bean.User;
import web.zz.service.LogService;
import web.zz.service.UserService;
import web.zz.utils.HttpContextUtil;
import web.zz.utils.IpUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired(required=false)
    private UserService userService;
    private LogService logService;

//    @RequestMapping(value = "/index.html")
//    public String loginPage(){
//        return "login";
//    }
//
////    @RequestMapping(value = "/loginCheck.html")
//    public ModelAndView loginCheck(HttpServletRequest request,LoginCommand loginCommand){
//        boolean isValidUser = userService.hasMatchUser(loginCommand.getUsername(),loginCommand.getPassword());
//        if (!isValidUser){
//            return new ModelAndView("login","error","用户或者密码错误");
//        }else {
//            User user = userService.findUser(loginCommand.getUsername());
//            request.getSession().setAttribute("user",user);
//            return new ModelAndView("main");
//        }
//    }
//    @RequestMapping(value = {"index"})
//    public String index(HttpServletRequest request,String username,String password){
//        return "index";
//    }

//    @RequestMapping(value = {"login"})
//    public ModelAndView login(HttpServletRequest request, @RequestParam("username") String username, @RequestParam("password")String password){
//        ModelAndView modelAndView = new ModelAndView();
//        User user = userService.findUser(username);
//        if (null!=user){
//            if (user.getPassword().equals(password)){
//                modelAndView.addObject("user",user);
//                modelAndView.setViewName("main");
//            }else {
//                modelAndView.setViewName("index");
//            }
//        }else {
//            modelAndView.setViewName("index");
//        }
//        return modelAndView;
//    }

//    @GetMapping(value = "login")
//    public String getPageHello(){
//        return "index";
//    }

//    @PostMapping(value = "login")
    @RequestMapping(value = "/login")
    //加上此注解，代表对此接口记录日志
    @LogAnnotation(module="登录",operator="获取用户信息")
    public String login(String username, String password, ModelMap modelMap) throws SQLException, ClassNotFoundException, IOException {
//        User user = new User();
//        user.setUsername(username);
//        user.setPassword(password);
        User user1 = userService.findUser(username);
        if(null!=user1){
            if (user1.getPassword().equals(password)){
                modelMap.put("username", username);
                modelMap.put("password", password);
                HttpServletRequest request = HttpContextUtil.getHttpServletRequest();
                modelMap.put("ip", IpUtils.getIpAddr(request));

                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time = df.format(System.currentTimeMillis());
                modelMap.put("time",time);
//                Log_info logInfo = logService.insert(username);
                return "main";
            }else {
                modelMap.put("msg", "密码错误！");
                return "index";
            }
        }
        else{
            modelMap.put("msg", "账号错误！");
            return "index";
        }
    }


    @RequestMapping(value = "/index")
    public void logout(Log_info loginInfo) throws SQLException, ClassNotFoundException {
        logService.insert(loginInfo);
    }
}
